//
//  JOTaBarButton.m
//  新浪微博模拟
//
//  Created by song on 15/3/30.
//  Copyright (c) 2015年 song. All rights reserved.
//

#define  JOImageRatio 0.6
#import "JOTaBarButton.h"
#import "UIImage+JO.h"

@interface JOTaBarButton ()

/**
  提醒数字
 **/
@property(nonatomic  ,weak) UIButton *badgeButton;

@end

@implementation JOTaBarButton


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView.contentMode =UIViewContentModeCenter;
        self.titleLabel.textAlignment =NSTextAlignmentCenter;
        self.titleLabel.font =[UIFont systemFontOfSize:11];
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar_slider"] forState:UIControlStateSelected];
        UIButton *badgeButton = [[UIButton alloc] init];
        badgeButton.hidden = YES;
        [badgeButton setBackgroundImage:[UIImage resizeImageWithName:@"main_badge"] forState:UIControlStateNormal];
        
        badgeButton.titleLabel.font  = [UIFont systemFontOfSize:10];
        badgeButton.userInteractionEnabled = NO;
        badgeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
        [self addSubview:badgeButton];
        self.badgeButton  = badgeButton;
    }
    
    return self;
}


-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = contentRect.size.height  * JOImageRatio;
    
    return CGRectMake(0, 0, imageW, imageH);
}



-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY =  contentRect.size.height * JOImageRatio;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height  -titleY;
    
    return  CGRectMake(0, titleY, titleW, titleH);
}

-(void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    [self setTitle:item.title forState:UIControlStateNormal];
    [self setImage:item.image forState:UIControlStateNormal];
    [self setImage:item.selectedImage forState:UIControlStateSelected];
//    [self setBackgroundImage:[UIImage imageNamed:@"tabbar_slider"] forState:UIControlStateSelected];
   
    // 设置提醒数字
    if(item.badgeValue)
    {
        self.badgeButton.hidden =NO;
        
        
        [self.badgeButton setTitle:item.badgeValue forState:UIControlStateNormal];
        
        CGFloat badgeY = 5;
        CGSize badgeSize = [item.badgeValue sizeWithFont:self.badgeButton.titleLabel.font];
        CGFloat badgeW = badgeSize.width +10;
        CGFloat badgeX = self.frame.size.width -badgeW -5;
        CGFloat badgeH = self.badgeButton.currentBackgroundImage.size.height;
        
        self.badgeButton.frame = CGRectMake(badgeX, badgeY, badgeW, badgeH);

        
    }else{
        self.badgeButton.hidden = YES;
    }
}


@end
